<template>
    <div class="container">
        <div ref="masonryContainer" class="masonry-container">
            <div v-for="item in 10" :key="item" class="redcommendCard">
                <img width="100%" :src="wallpaperList[item%2]" alt="">
            </div>
        </div>
    </div>
</template>

<script>
import Masonry from 'masonry-layout';

export default {
    data(){
        return{
            wallpaperList:[
                "https://img-baofun.zhhainiao.com/pcwallpaper_ugc/static/6512ad38e52a9c1b9cf429ee69602f16.jpg?x-oss-process=image%2fresize%2cm_lfit%2cw_2560%2ch_1440",
                "https://pic4.zhimg.com/v2-2ee6c613b0f63876c54811b96199fb83_b.jpg"
            ]
        }
    },
    mounted() {
        // 在组件挂载后初始化 Masonry
        this.masonry = new Masonry(this.$refs.masonryContainer, {
            itemSelector: '.redcommendCard',
            columnWidth: 180,
            gutter: 10
        });
    },
    beforeUnmount() {
        // 在组件卸载前销毁 Masonry 实例
        if (this.masonry) {
            this.masonry.destroy();
        }
    }
}
</script>

<style scoped>
.container {
    margin: 0 auto;
}

.masonry-container {
    /* 隐藏容器溢出部分 */
    overflow: hidden;
}

.redcommendCard {
    width: 180px;
    padding: 5px;
    box-sizing: border-box;
    height: fit-content;
    background: rebeccapurple;
    margin-bottom: 10px;
}
</style>